package com.abel.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author xiachao
 * @date 2019/2/3
 */
public class Task implements Runnable{

    private static Lock lock = new ReentrantLock();

    private static ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private int num;

    public Task(int num){
        this.num = num;
    }
    @Override
    public void run() {
        try {
            exe(num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void exe(int num) throws InterruptedException {
        if(lock.tryLock(num, TimeUnit.SECONDS)){
            try{
                for(int i=1;i<=10;i++){
                    System.out.println(num+" - "+i);
                }
            }finally {
                lock.unlock();
            }

        }else{
            System.out.println(num+" get lock failed");
        }

    }
}
